package com.example.petstore.aspect;

import jakarta.servlet.http.HttpSession;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Aspect
@Component
public class LoginAdvice {
//    @Pointcut("execution(* com.example.petstore.controller.UserController.changeInfo(..)) ")
//    public void userLoginPointcut() {}
//
//    @Pointcut("execution(* com.example.petstore.controller.CartController.userAddToCart(..))")
//    public void userCartPointcut() {}
//
//    @Pointcut("execution(* com.example.petstore.controller.ItemController.itemManager(..))")
//    public void adminLoginPointcut() {}
//
//    @Around("userLoginPointcut()")
//    public Object checkUserLoginChang(ProceedingJoinPoint joinPoint) throws Throwable{
////        return userLoginCheck(joinPoint)
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
//        HttpSession session = attributes.getRequest().getSession(false);
//        if (session != null && session.getAttribute("loginUser") != null) {
//            Object result = joinPoint.proceed();
//            return result;
//        } else {
//            return "test_index";
//            // todo:如果没有user，重定向到登录页面
//        }
//    }
//
//    @Around("userCartPointcut()")
//    public Object checkUserLoginCart(ProceedingJoinPoint joinPoint) throws Throwable{
////        return userLoginCheck(joinPoint)
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
//        HttpSession session = attributes.getRequest().getSession(false);
//        if (session != null && session.getAttribute("loginUser") != null) {
//            Object result = joinPoint.proceed();
//            return result;
//        } else {
//            return "test_index";
//            // todo:如果没有user，重定向到登录页面
//        }
//    }
//
//    @Around("adminLoginPointcut()")
//    public Object checkAdminLoginCart(ProceedingJoinPoint joinPoint) throws Throwable{
////        return userLoginCheck(joinPoint)
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
//        HttpSession session = attributes.getRequest().getSession(false);
//        if (session != null && session.getAttribute("loginAdmin") != null) {
//            Object result = joinPoint.proceed();
//            return result;
//        } else {
//            return "test_index";
//            // todo:如果没有user，重定向到登录页面
//        }
//    }
}
